/**
 * 
 */
package org.vsg.stock.dzh;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.vsg.common.configration.PropertiesSupport;
import org.vsg.stock.basic.persistence.StockCategoryDao;
import org.vsg.stock.extension.service.StockMigrationService;


/**
 * @author vison
 *
 */
public class CleanAndImportCategory {

	private final static Logger logger = LoggerFactory.getLogger(CleanAndImportCategory.class);	
	
	
	public void exec(ApplicationContext appContext) throws Exception {
		
		PropertiesSupport proSupport = (PropertiesSupport)appContext.getBean("propertyConfigurer");
		
		String propBlock = proSupport.getProperties().getProperty("category.stock.block");
		if (propBlock == null) {
			logger.info("Property 'category.stock.block' is not defined! ");
		}
		
		String vsatDir = proSupport.getProperties().getProperty("category.stock.vsat");
		if (propBlock == null) {
			logger.info("Property 'category.stock.vsat' is not defined! ");
		}
		
		StockMigrationService stkMigraService = (StockMigrationService)appContext.getBean("stockMigrationService");
		
		StockCategoryDao stkCategoryDao = (StockCategoryDao)appContext.getBean("stockCategoryDao");

		stkCategoryDao.cleanAllAndMapping();
		
		
		// --- import data ---
		File blockDir = new File(propBlock);
		
		stkMigraService.updateDZHBlockToStockCategoryFromFile(blockDir);
		
		// --- import concept ---
		File vsatD = new File(vsatDir);
		stkMigraService.updateStockCategoryFromVsatData(vsatD);


		// ---- update data mapping ----
		
		stkMigraService.updateStockAndCategoryMapFromDZHFile(blockDir);
		
		stkMigraService.updateStockAndCategoryMapFromVsatData( vsatD );



	}
	
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ClassLoader clsLoader = CleanAndImportCategory.class.getClassLoader();
		// --- run the main page ---
		// build the read the path , dzh's home path
		String[] configContextFiles = {
				"classpath:/spring/configurationContext.xml",
				"classpath*:/spring/applicationContext.xml",
				"classpath*:/spring/dataAccessContext.xml"
		};
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configContextFiles,true);
		
	
		CleanAndImportCategory mainProg = new CleanAndImportCategory();
		
		try {
			mainProg.exec(ctx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		logger.info("program run ... Ok!");
		



	}

}
